********************************************************************************
** 	TITLE:		UK15_results                                                  ** 
**	AUTHOR:	    Philippe Mongrain                                             **
**	DATA:       uk15                                                          **
**	VERSION:	Stata 16					                                  **
**	DATE:		October 2022  				                                  **
********************************************************************************

* Version control

version 16.0

* Open log file

capture log close
log using "UK15_results.smcl", replace

* Import the dataset

copy https://researchbriefings.files.parliament.uk/documents/CBP-7186/hocl-ge2015-results-full.csv uk15.csv, replace
type uk15.csv
import delimited uk15.csv, clear

* Generate rank of parties

gsort constituency_name -votes
bysort constituency_name : gen rank = _n

gen first = party_name if rank == 1
gen second = party_name if rank == 2
gen third = party_name if rank == 3

bysort constituency_name : gen winner15 = first[1]
bysort constituency_name : gen runnerup15 = second[2]
bysort constituency_name : gen thirdplace15 = third[3]

* Generate margins of victory

bysort constituency_name : gen margin = (share[1] - share[2])*100

* Generate effective number of electoral parties

gen sqrvotepr = (share)^2

bysort constituency_name : egen sumsqrvotepr = sum(sqrvotepr)

gen enep = 1/sumsqrvotepr

* Labelling parties

replace party_name = "1" if party_name == "Conservative"
replace party_name = "2" if party_name == "Labour"
replace party_name = "3" if party_name == "Liberal Democrat"
replace party_name = "4" if party_name == "Scottish National Party"
replace party_name = "5" if party_name == "Plaid Cymru"
replace party_name = "6" if party_name == "UK Independence Party"
replace party_name = "7" if party_name == "Green"
replace party_name = "8" if party_name == "British National Party"
replace party_name = "9" if party_name!="1" & party_name!="2" & party_name!="3" & party_name!="4" & party_name!="5" & party_name!="6" & party_name!="7"

destring (party_name), replace

replace winner15 = "1" if winner15 == "Conservative"
replace winner15 = "2" if winner15 == "Labour"
replace winner15 = "3" if winner15 == "Liberal Democrat"
replace winner15 = "4" if winner15 == "Scottish National Party"
replace winner15 = "5" if winner15 == "Plaid Cymru"
replace winner15 = "6" if winner15 == "UK Independence Party"
replace winner15 = "7" if winner15 == "Green"
replace winner15 = "8" if winner15 == "British National Party"
replace winner15 = "9" if winner15!="1" & winner15!="2" & winner15!="3" & winner15!="4" & winner15!="5" & winner15!="6" & winner15!="7"

destring (winner15), replace

replace runnerup15 = "1" if runnerup15 == "Conservative"
replace runnerup15 = "2" if runnerup15 == "Labour"
replace runnerup15 = "3" if runnerup15 == "Liberal Democrat"
replace runnerup15 = "4" if runnerup15 == "Scottish National Party"
replace runnerup15 = "5" if runnerup15 == "Plaid Cymru"
replace runnerup15 = "6" if runnerup15 == "UK Independence Party"
replace runnerup15 = "7" if runnerup15 == "Green"
replace runnerup15 = "8" if runnerup15 == "British National Party"
replace runnerup15 = "9" if runnerup15!="1" & runnerup15!="2" & runnerup15!="3" & runnerup15!="4" & runnerup15!="5" & runnerup15!="6" & runnerup15!="7"

destring (runnerup15), replace

replace thirdplace15 = "1" if thirdplace15 == "Conservative"
replace thirdplace15 = "2" if thirdplace15 == "Labour"
replace thirdplace15 = "3" if thirdplace15 == "Liberal Democrat"
replace thirdplace15 = "4" if thirdplace15 == "Scottish National Party"
replace thirdplace15 = "5" if thirdplace15 == "Plaid Cymru"
replace thirdplace15 = "6" if thirdplace15 == "UK Independence Party"
replace thirdplace15 = "7" if thirdplace15 == "Green"
replace thirdplace15 = "8" if thirdplace15 == "British National Party"
replace thirdplace15 = "9" if thirdplace15!="1" & thirdplace15!="2" & thirdplace15!="3" & thirdplace15!="4" & thirdplace15!="5" & thirdplace15!="6" & thirdplace15!="7"

destring (thirdplace15), replace

label define party 1 "Conservative" 2 "Labour" 3 "Liberal Democrat" 4 "SNP" 5 "Plaid Cymru" 6 "UKIP" 7 "Green" 8 "BNP" 9 "Other"
label values party_name winner15 runnerup15 thirdplace15 party
 
* Selecting, ordering, and sorting variables

keep ons_id constituency_name party_name rank votes share margin enep winner15 runnerup15 thirdplace15

order ons_id constituency_name party_name rank votes share margin enep winner15 runnerup15 thirdplace15

sort ons_id rank

drop if rank > 3

* Drop duplicates

duplicates tag ons_id, gen(dup)
duplicates drop ons_id, force
drop dup

* Change constituency ID

replace ons_id = subinstr(ons_id,"E1400","1",.)
replace ons_id = subinstr(ons_id,"S1400","2",.)
replace ons_id = subinstr(ons_id,"W0700","3",.)
replace ons_id = subinstr(ons_id,"N0600","4",.)

* Create election variable

gen election15 = "2015 British general election"

* Saving file

save UK15_results, replace

* Merge with UK10_results.dta and generate reelection variable

merge 1:1 _n using UK10_results.dta

* Generate variable for incumbent party reelection

gen reelected15 = 1 if winner15 == winner10
replace reelected15 = 0 if reelected15!=1

* Saving file

save UK1015_results, replace

log close